package chenji.yourticket.press;

import java.util.List;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import chenji.yourticket.Main;
import chenji.yourticket.inventory.SimpleInventory;
import chenji.yourticket.inventory.SimpleItemStack;
import chenji.yourticket.inventory.InventoryManager.OpeningInventoryType;
import chenji.yourticket.model.ModelInfo;

public class PressInventory extends SimpleInventory {
	Player p;
	ModelInfo modelinfo;
	int MaxAmount;
	int nowAmount;

	public PressInventory(Player p, ModelInfo modelinfo) {
		super(p, (short) 7, Main.getMessageManager().getPress_Title(), 4);
		this.p = p;
		this.modelinfo = modelinfo;
		MaxAmount = Main.getPressManager().getMaxAmount(p, modelinfo);
		installItemStack();
		this.upDateItemStack();
		this.openInventory(OpeningInventoryType.PressManager);
	}

	@Override
	public void installItemStack() {
		this.getInventory().setItem(13, this.modelinfo.getItem());
		ItemStack ia = new SimpleItemStack(Material.STAINED_GLASS_PANE,
				Main.getMessageManager().getItem_NowStyle_Press_Lore(),
				Main.getMessageManager().getItem_NowStyle_Press_Title(), (short) 4);

		int[] slot;
		slot = new int[] { 12, 14, 21, 22, 23 };
		for (int i : slot) {
			this.getInventory().setItem(i, ia);
		}
		ia = new SimpleItemStack(Material.BOOK_AND_QUILL, Main.getMessageManager().getItem_Edit_Lore(),
				Main.getMessageManager().getItem_Edit_Title());
		this.getInventory().setItem(10, ia);
		ia = new SimpleItemStack(Material.FIREBALL, Main.getMessageManager().getItem_Back_Lore(),
				Main.getMessageManager().getItem_Back_Title());
		this.getInventory().setItem(16, ia);
	}

	public void upDateItemStack() {
		double allPrice = this.nowAmount * Main.getMaterialManager().getPrice(modelinfo.getItem().getType())
				* Main.getVIPDiscountManager().getDiscount(p);
		double balance = Main.getEconomy().getBalance(p);
		String VIPGroup = Main.getVIPDiscountManager().getGroup(p);
		double discount = Main.getVIPDiscountManager().getDiscount(p);
		String AddLore;
		if (VIPGroup == null) {
			AddLore = null;
		} else {
			AddLore = Main.getMessageManager().getItem_Add_Lore_VIP().replaceAll("%group%", VIPGroup)
					.replaceAll("%discount%", Double.toString(discount * 100) + "%");
		}

		ItemStack i;
		if (this.nowAmount == 0) {
			i = new SimpleItemStack(Material.STAINED_GLASS_PANE, Main.getMessageManager().getItem_CannotPress_Lore(),
					Main.getMessageManager().getItem_CannotPress_Title(), (short) 14);
		} else {
			i = new SimpleItemStack(Material.INK_SACK, "", Main.getMessageManager().getItem_clickToPress());
		}
		this.getInventory().setItem(19, i);
		if (this.nowAmount >= this.MaxAmount) {
			i = new SimpleItemStack(Material.WOOL,
					Main.getMessageManager().getItem_Add_Lore().replaceAll("%nowAmount%", Integer.toString(nowAmount))
							.replaceAll("%maxamount%", Integer.toString(this.MaxAmount))
							.replaceAll("%price%", Double.toString(allPrice))
							.replaceAll("%balance%", Double.toString(balance)),
					Main.getMessageManager().getItem_Add_Title(), (short) 14);
			this.getInventory().setItem(11, this.addLore(i, AddLore));

		} else {
			i = new SimpleItemStack(Material.WOOL,
					Main.getMessageManager().getItem_Add_Lore().replaceAll("%nowAmount%", Integer.toString(nowAmount))
							.replaceAll("%maxamount%", Integer.toString(this.MaxAmount))
							.replaceAll("%price%", Double.toString(allPrice)).replaceAll("%balance%",
									Double.toString(balance)),
					Main.getMessageManager().getItem_Add_Title(), (short) 5);
			this.getInventory().setItem(11, this.addLore(i, AddLore));
		}

		if (this.nowAmount + 10 > MaxAmount) {
			i = new SimpleItemStack(Material.WOOL,
					Main.getMessageManager().getItem_Add10_Lore().replaceAll("%nowAmount%", Integer.toString(nowAmount))
							.replaceAll("%maxamount%", Integer.toString(this.MaxAmount))
							.replaceAll("%price%", Double.toString(allPrice))
							.replaceAll("%balance%", Double.toString(balance)),
					Main.getMessageManager().getItem_Add10_Title(), (short) 14);
			this.getInventory().setItem(20, this.addLore(i, AddLore));

		} else {
			i = new SimpleItemStack(Material.WOOL,
					Main.getMessageManager().getItem_Add10_Lore().replaceAll("%nowAmount%", Integer.toString(nowAmount))
							.replaceAll("%maxamount%", Integer.toString(this.MaxAmount))
							.replaceAll("%price%", Double.toString(allPrice))
							.replaceAll("%balance%", Double.toString(balance)),
					Main.getMessageManager().getItem_Add10_Title(), (short) 5);
			this.getInventory().setItem(20, this.addLore(i, AddLore));
		}

		if (this.nowAmount <= 0) {
			i = new SimpleItemStack(Material.WOOL, Main.getMessageManager().getItem_Lessen_Lore()
					.replaceAll("%nowAmount%", Integer.toString(nowAmount))
					.replaceAll("%maxamount%", Integer.toString(this.MaxAmount))
					.replaceAll("%price%", Double.toString(allPrice)).replaceAll("%balance%", Double.toString(balance)),
					Main.getMessageManager().getItem_Lessen_Title(), (short) 14);
			this.getInventory().setItem(15, this.addLore(i, AddLore));
		} else {
			i = new SimpleItemStack(Material.WOOL,
					Main.getMessageManager().getItem_Add_Lore().replaceAll("%nowAmount%", Integer.toString(nowAmount))
							.replaceAll("%maxamount%", Integer.toString(this.MaxAmount))
							.replaceAll("%price%", Double.toString(allPrice))
							.replaceAll("%balance%", Double.toString(balance)),
					Main.getMessageManager().getItem_Lessen_Title(), (short) 5);
			this.getInventory().setItem(15, this.addLore(i, AddLore));

		}
		if (this.nowAmount - 10 < 0) {
			i = new SimpleItemStack(Material.WOOL, Main.getMessageManager().getItem_Lessen10_Lore()
					.replaceAll("%nowAmount%", Integer.toString(nowAmount))
					.replaceAll("%maxamount%", Integer.toString(this.MaxAmount))
					.replaceAll("%price%", Double.toString(allPrice)).replaceAll("%balance%", Double.toString(balance)),
					Main.getMessageManager().getItem_Lessen10_Title(), (short) 14);
			this.getInventory().setItem(24, this.addLore(i, AddLore));
		} else {
			i = new SimpleItemStack(Material.WOOL, Main.getMessageManager().getItem_Lessen10_Lore()
					.replaceAll("%nowAmount%", Integer.toString(nowAmount))
					.replaceAll("%maxamount%", Integer.toString(this.MaxAmount))
					.replaceAll("%price%", Double.toString(allPrice)).replaceAll("%balance%", Double.toString(balance)),
					Main.getMessageManager().getItem_Lessen10_Title(), (short) 5);
			this.getInventory().setItem(24, this.addLore(i, AddLore));
		}

	}

	public ItemStack addLore(ItemStack i, String lore) {
		if (lore == null) {
			return i;
		}
		ItemMeta m = i.getItemMeta();
		List<String> lr = m.getLore();
		lr.add(lore);
		m.setLore(lr);
		i.setItemMeta(m);
		return i;
	}

	private void giveItem() {

		ItemStack i = this.modelinfo.getItem().clone();
		i.setAmount(this.nowAmount);
		p.getInventory().addItem(i);

	}

	public void clickInventory(int slot) {
		if (slot == 11) {
			if (this.nowAmount >= this.MaxAmount) {
				return;
			}
			this.nowAmount++;
			upDateItemStack();
		}
		if (slot == 20) {
			if (this.nowAmount + 10 > MaxAmount) {
				return;
			}
			this.nowAmount = this.nowAmount + 10;
			this.upDateItemStack();
		}
		if (slot == 15) {
			if (this.nowAmount <= 0) {
				return;
			}
			this.nowAmount--;
			this.upDateItemStack();
		}
		if (slot == 24) {
			if (this.nowAmount - 10 < 0) {
				return;
			}
			this.nowAmount = this.nowAmount - 10;
			this.upDateItemStack();
		}
		if (slot == 19) {
			if (this.nowAmount <= 0) {
				return;
			}
			this.giveItem();
			Main.getEconomy().withdrawPlayer(p,
					this.nowAmount * Main.getMaterialManager().getPrice(this.modelinfo.getItem().getType())
							* Main.getVIPDiscountManager().getDiscount(p));
			p.closeInventory();
		}
		if (slot == 16) {
			Main.getModelManager().openInventory(p);
		}
	}

}
